package com.tansun.rule.utils;

import java.util.UUID;

/**
 * @author CHEN SHUN FANG
 * @date 2020/03/20 18:44
 */
public class Uuid {
    private static int _machineID = 0;
    private static int _incID = 0;
	public static final String TRAN_SUCCESS = "1";

    public static String createId() {
        String uuid = String.valueOf(Uuid.ObjectID());
        return uuid;
    }

    private static int getMachineID() {
        //复杂些的可以通过主机主板序列号、IP、硬盘序列号等生成机器码，简单起见默认为１
        _machineID += 1;
        return _machineID % 127;
    }

    private static int getInc() {
        //一般使用Memcached统一维护一个全局自增ID，简单起见默认为１
        _incID += 1;
        return _incID;
    }

    public static long ObjectID() {
        long v_time = (int) (System.currentTimeMillis() / 1000);
        int v_machine = getMachineID();
        int v_inc = getInc();
        long myUUID = 0;
        myUUID = myUUID | v_inc;
        myUUID = myUUID | (v_machine << 24);
        myUUID = myUUID | (v_time << 32);
        return myUUID;
    }

    public static void reverseObjectID(long myUUID) {
        //反向解析myUUID
        int v_inc = (int) myUUID & 0xFFFFFF;//取出最右边3字节（即24bit）的值，每个16进制数存储4bit
        int v_machine = (int) (myUUID >>> 24) & 0xFF;//无符号右移24位，然后取最右边1字节的值
        long v_time = (myUUID >>> 32) & 0xFFFFFFFF;//无符号右移32位，然后取四字节的秒数
        System.out.println("v_time=" + Long.toHexString(v_time));
        System.out.println("时间为：" + new java.util.Date(v_time * 1000));
        System.out.println("v_machine=" + v_machine);
        System.out.println("inc=" + v_inc);
    }

    public static String createSign() {
        return UUID.randomUUID().toString().replace("-", "").substring(10);
    }
}
